# !/usr/bin/env python
# -*-coding:utf-8 -*-
"""
# File       : orderlines_recover_view.py
# Time       ：2023/8/18 22:33
# Author     ：Y-aong
# version    ：python 3.7
# Description：
    继续流程
"""
import traceback

from flask import request
from flask_restx import fields

from apis.orderlines import orderlines_ns
from public.api_utils.api_decorator import handle_api_error
from public.base_response import generate_response, MarshalModel
from public.base_view import AbstractView
from public.logger import logger

recover = {
    'process_instance_id': fields.String(description='流程实例ID'),
    'recover_schedule': fields.Boolean(description='是否恢复定时')
}
orderlines_recover_marshal = MarshalModel('recover', orderlines_ns)


class OrderlinesRecoverView(AbstractView):
    url = '/recover'

    def __init__(self, *args, **kwargs):
        super(OrderlinesRecoverView, self).__init__(*args, **kwargs)
        self.form_data = request.json
        self.is_logger = True

    @handle_api_error()
    @orderlines_ns.expect(orderlines_recover_marshal.detail_request(recover))
    @orderlines_ns.marshal_with(orderlines_recover_marshal.create_response(True))
    def post(self):
        """流程恢复"""
        try:
            from orderlines import OrderLines

            process_instance_id = self.form_data.get('process_instance_id')
            recover_schedule = self.form_data.get('recover_schedule')
            OrderLines().recover_process(process_instance_id, recover_schedule)
            return generate_response(message='process begin recover')
        except Exception as e:
            logger.error(f'流程恢复失败:{e}\n {traceback.format_exc()}')
            return generate_response(code=500, message='process recover failure')
